package com.funtl.hello.httpclient;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author tw
 * @version 1.0
 * @date 2020/6/8 21:02
 */
public class JsonTester {
    public static void main(String[] args) {

        //testPojoJson();

//        //创建objectMapper
//        ObjectMapper objectMapper = new ObjectMapper();
//        String json = "{\"draw\":1,\"recordsTotal\":21,\"recordsFiltered\":21,\"data\":[{\"id\":52,\"created\":1591100476000,\"updated\":1591100476000,\"username\":\"哒哒哒哒哒哒\",\"phone\":\"13862663619\",\"email\":\"tes111t@te111st.com\"},{\"id\":51,\"created\":1591012039000,\"updated\":1591012039000,\"username\":\"asadas\",\"phone\":\"13162663619\",\"email\":\"tes22t@test22.com\"},{\"id\":44,\"created\":1587835686000,\"updated\":1589204335000,\"username\":\"qqq11111\",\"phone\":\"13062663617\",\"email\":\"weqw@11.com\"},{\"id\":50,\"created\":1588006450000,\"updated\":1588006450000,\"username\":\"te11st@test.com\",\"phone\":\"13562663618\",\"email\":\"1234@ss.com\"},{\"id\":49,\"created\":1588005369000,\"updated\":1588005369000,\"username\":\"test@test.com1112\",\"phone\":\"13062663611\",\"email\":\"23@123.com\"},{\"id\":48,\"created\":1588004610000,\"updated\":1588004610000,\"username\":\"test@test.com\",\"phone\":\"13062663619\",\"email\":\"test@test.com\"},{\"id\":35,\"created\":1438402913000,\"updated\":1438402913000,\"username\":\"test01\",\"phone\":\"15600876321\",\"email\":null},{\"id\":34,\"created\":1438402421000,\"updated\":1438402421000,\"username\":\"niuniu4\",\"phone\":\"15877680983\",\"email\":null},{\"id\":33,\"created\":1438402106000,\"updated\":1438402106000,\"username\":\"niuniu3\",\"phone\":\"14322334456\",\"email\":null},{\"id\":32,\"created\":1438401890000,\"updated\":1438401890000,\"username\":\"niuniu2\",\"phone\":\"14322334455\",\"email\":null}],\"error\":null}";
//
//        try {
//            User user = objectMapper.readValue(json, User.class);
//            System.out.println(user);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }



        ObjectMapper objectMapper = new ObjectMapper();
        String json = "{\"draw\":1,\"recordsTotal\":21,\"recordsFiltered\":21,\"data\":[{\"id\":52,\"created\":1591100476000,\"updated\":1591100476000,\"username\":\"哒哒哒哒哒哒\",\"phone\":\"13862663619\",\"email\":\"tes111t@te111st.com\"},{\"id\":51,\"created\":1591012039000,\"updated\":1591012039000,\"username\":\"asadas\",\"phone\":\"13162663619\",\"email\":\"tes22t@test22.com\"},{\"id\":44,\"created\":1587835686000,\"updated\":1589204335000,\"username\":\"qqq11111\",\"phone\":\"13062663617\",\"email\":\"weqw@11.com\"},{\"id\":50,\"created\":1588006450000,\"updated\":1588006450000,\"username\":\"te11st@test.com\",\"phone\":\"13562663618\",\"email\":\"1234@ss.com\"},{\"id\":49,\"created\":1588005369000,\"updated\":1588005369000,\"username\":\"test@test.com1112\",\"phone\":\"13062663611\",\"email\":\"23@123.com\"},{\"id\":48,\"created\":1588004610000,\"updated\":1588004610000,\"username\":\"test@test.com\",\"phone\":\"13062663619\",\"email\":\"test@test.com\"},{\"id\":35,\"created\":1438402913000,\"updated\":1438402913000,\"username\":\"test01\",\"phone\":\"15600876321\",\"email\":null},{\"id\":34,\"created\":1438402421000,\"updated\":1438402421000,\"username\":\"niuniu4\",\"phone\":\"15877680983\",\"email\":null},{\"id\":33,\"created\":1438402106000,\"updated\":1438402106000,\"username\":\"niuniu3\",\"phone\":\"14322334456\",\"email\":null},{\"id\":32,\"created\":1438401890000,\"updated\":1438401890000,\"username\":\"niuniu2\",\"phone\":\"14322334455\",\"email\":null}],\"error\":null}";
        try {
            JsonNode jsonNode = objectMapper.readTree(json);
            JsonNode data = jsonNode.findPath("data");
            System.out.println(data);

//            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class,TbUser.class);
//            List<TbUser> tbUsers = objectMapper.readValue(data.toString(),javaType);
            // 或者
            List<TbUser> tbUsers = objectMapper.readValue(data.toString(), new TypeReference<List<TbUser>>() {});
            for (TbUser tbUser : tbUsers) {
                System.out.println(tbUser);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void testPojoJson() {
        //创建objectMapper
        ObjectMapper objectMapper = new ObjectMapper();
        // Json 转 java
        String json = "{\"draw\": 1,\"recordsTotal\": 21,\"recordsFiltered\": 21,\"error\": null}";
        try {
            User user = objectMapper.readValue(json, User.class);
            System.out.println(user);
            System.out.println(objectMapper.writeValueAsString(user));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

class User {
    private int aa;
    private int draw;
    private int recordsTotal;
    private int recordsFiltered;
    private List<TbUser> data;
    private String error;

    public List<TbUser> getData() {
        return data;
    }

    public void setData(List<TbUser> data) {
        this.data = data;
    }

    public int getAa() {
        return aa;
    }

    public void setAa(int aa) {
        this.aa = aa;
    }

    public int getDraw() {
        return draw;
    }

    public void setDraw(int draw) {
        this.draw = draw;
    }

    public int getRecordsTotal() {
        return recordsTotal;
    }

    public void setRecordsTotal(int recordsTotal) {
        this.recordsTotal = recordsTotal;
    }

    public int getRecordsFiltered() {
        return recordsFiltered;
    }

    public void setRecordsFiltered(int recordsFiltered) {
        this.recordsFiltered = recordsFiltered;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    @Override
    public String toString() {
        return "User{" +
                "aa=" + aa +
                ", draw=" + draw +
                ", recordsTotal=" + recordsTotal +
                ", recordsFiltered=" + recordsFiltered +
                ", data=" + data +
                ", error='" + error + '\'' +
                '}';
    }
}

class TbUser {
    private Long id;
    private Date created;
    private Date updated;
    private String username;
    private String phone;
    private String email;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "TbUser{" +
                "id=" + id +
                ", created=" + created +
                ", updated=" + updated +
                ", username='" + username + '\'' +
                ", phone='" + phone + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}
